//
//  NewsViewController.m
//  HealthA段
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsViewController.h"
#import "Medicine.h"
#import "Life.h"
#import "Drug.h"
#import "foodModel.h"
#import "DiseaseModel.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface NewsViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)NSArray *headArr;
@property(nonatomic,retain)UIScrollView *headScrollerView;
@property(nonatomic,retain)UIScrollView *mainScrollerView;
@property(nonatomic,retain)UIView *medicineView;//医药新闻视图
@property(nonatomic,retain)UIView *lifeView;//生活贴士视图
@property(nonatomic,retain)UIView *drugView;//药品新闻视图
@property(nonatomic,retain)UIView *foodView;//食品新闻视图
@property(nonatomic,retain)UIView *diseaseView;//疾病快讯视图
@property(nonatomic,retain)UIScrollView *medScrollerView;
@property(nonatomic,retain)UITableView *medTableView;
@property(nonatomic,retain)NSMutableArray *meiArr;
@end

@implementation NewsViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.headArr = @[@"医药新闻",@"生活贴士",@"药品新闻",@"食品新闻",@"疾病快讯"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

   //创建导航分类视图
    self.headScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0.1*WIDTH)];
    self.headScrollerView.contentSize = CGSizeMake(WIDTH, 0.1*WIDTH);
    [self.view addSubview:self.headScrollerView];
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:self.headArr[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(i*0.2*WIDTH, 0, 0.2*WIDTH, 0.1*WIDTH);
        button.layer.cornerRadius = 20;
        button.tag = i + 100;
        button.tintColor = [UIColor blackColor];
        [[self.view viewWithTag:100]setTintColor:[UIColor redColor]];
        [button addTarget:self action:@selector(ClickHeadBu:) forControlEvents:UIControlEventTouchUpInside];
        [self.headScrollerView addSubview:button];
    }
    //创建主视图
    self.mainScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0.1*WIDTH, WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.mainScrollerView.contentSize = CGSizeMake(5*WIDTH, HEIGHT-64-(0.1)*WIDTH-49);
    self.mainScrollerView.pagingEnabled = YES;
    self.mainScrollerView.bounces = NO;
    [self.view addSubview:self.mainScrollerView];
    //设置代理
    self.mainScrollerView.delegate = self;
    //医药新闻视图
    self.medicineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.medicineView.backgroundColor = [UIColor yellowColor];
    [self.mainScrollerView addSubview:self.medicineView];
    self.medTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0.1*WIDTH, WIDTH, HEIGHT-64-(0.1)*WIDTH-49) style:UITableViewStylePlain];
    [self.medicineView addSubview:self.medTableView];
    self.medTableView.delegate = self;
    
    
    //生活贴士视图
    self.lifeView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH,0,WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.lifeView.backgroundColor = [UIColor orangeColor];
    [self.mainScrollerView addSubview:self.lifeView];
    
    //药品新闻视图
    self.drugView = [[UIView alloc] initWithFrame:CGRectMake(2*WIDTH, 0, WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.drugView.backgroundColor = [UIColor grayColor];
    [self.mainScrollerView addSubview:self.drugView];
    
    
    //食品新闻视图
    self.foodView = [[UIView alloc] initWithFrame:CGRectMake(3*WIDTH, 0, WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.foodView.backgroundColor = [UIColor greenColor];
    [self.mainScrollerView addSubview:self.foodView];
    
    //疾病快讯视图
    self.diseaseView = [[UIView alloc] initWithFrame:CGRectMake(4*WIDTH, 0, WIDTH, HEIGHT-64-(0.1)*WIDTH-49)];
    self.diseaseView.backgroundColor = [UIColor cyanColor];
    [self.mainScrollerView addSubview:self.diseaseView];
}

//导航分类栏Button的点击方法
- (void)ClickHeadBu:(UIButton *)button
{
    
    switch (button.tag) {
        case 100:
            self.mainScrollerView.contentOffset = CGPointMake(0, 0);
            [self ChangeButtonFontAndColorTag1:100 tag2:101 tag3:102 tag4:103 tag5:104];
            break;
        case 101:
            self.mainScrollerView.contentOffset = CGPointMake(WIDTH, 0);
            [self ChangeButtonFontAndColorTag1:101 tag2:100 tag3:102 tag4:103 tag5:104];
            break;
        case 102:
            self.mainScrollerView.contentOffset = CGPointMake(2*WIDTH, 0);
            [self ChangeButtonFontAndColorTag1:102 tag2:100 tag3:101 tag4:103 tag5:104];
            break;
        case 103:
            self.mainScrollerView.contentOffset = CGPointMake(3*WIDTH, 0);
            [self ChangeButtonFontAndColorTag1:103 tag2:100 tag3:101 tag4:102 tag5:104];
            break;
        case 104:
            self.mainScrollerView.contentOffset = CGPointMake(4*WIDTH, 0);
            [self ChangeButtonFontAndColorTag1:104 tag2:100 tag3:101 tag4:102 tag5:103];
            break;
    }
}

//结束滑动触发的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.mainScrollerView.contentOffset.x == 0) {
        [self ChangeButtonFontAndColorTag1:100 tag2:101 tag3:103 tag4:104 tag5:102];
    }
    if (self.mainScrollerView.contentOffset.x == WIDTH) {
        [self ChangeButtonFontAndColorTag1:101 tag2:100 tag3:102 tag4:103 tag5:104];
    }
    if (self.mainScrollerView.contentOffset.x == 2*WIDTH) {
        [self ChangeButtonFontAndColorTag1:102 tag2:100 tag3:101 tag4:103 tag5:104];
    }
    if (self.mainScrollerView.contentOffset.x == 3*WIDTH) {
        [self ChangeButtonFontAndColorTag1:103 tag2:100 tag3:101 tag4:102 tag5:104];
    }
    if (self.mainScrollerView.contentOffset.x == 4*WIDTH) {
        [self ChangeButtonFontAndColorTag1:104 tag2:100 tag3:101 tag4:102 tag5:103];
    }
}

- (void)ChangeButtonFontAndColorTag1:(NSInteger)tag1 tag2:(NSInteger)tag2 tag3:(NSInteger)tag3 tag4:(NSInteger)tag4 tag5:(NSInteger)tag5
{
    [[self.view viewWithTag:tag1] setTintColor:[UIColor redColor]];
    [[self.view viewWithTag:tag2] setTintColor:[UIColor blackColor]];
    [[self.view viewWithTag:tag3] setTintColor:[UIColor blackColor]];
    [[self.view viewWithTag:tag4] setTintColor:[UIColor blackColor]];
    [[self.view viewWithTag:tag5] setTintColor:[UIColor blackColor]];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
